<template>
  <lay-container fluid="true" style="padding: 10px">

    <lay-card>
          <lay-form style="margin-top: 20px">
            <lay-row>
              <lay-col :md="12">
               
                <lay-form-item label="关键字：" label-width="60">
                  <lay-input
                    v-model="qm.key" style="width: 90%" 
                    placeholder="姓名、所属机构"
                    :allow-clear="true"
                  ></lay-input>
                </lay-form-item>
              </lay-col>
              
               
              

              <lay-col :md="6">
                <lay-form-item label-width="0">
                  <lay-button type="primary" @click="toSearch">查询</lay-button>
                  <lay-button @click="toReset">重置</lay-button>
                </lay-form-item>
              </lay-col>
            </lay-row>
          </lay-form>
        </lay-card>
        <div class="maintable-box"  >
          <lay-table
          :height="'100%'"
            :page="page"
            :columns="columns"
            :dataSource="dataSource"
            :default-toolbar="defaultToolbar"
            v-model:selectedKeys="selectedKeys"
            even
            @row="rowClick"
            @Change="pageChange"
            @sortChange="sortChange"
          >
            <template v-slot:toolbar>
              <lay-row>
                <lay-col :md="18">
                   <!-- <lay-button size="sm" type="primary"
                    @click="addRow"
                    v-permission="['/htuser/insert']"
                    >添加</lay-button> -->
                  <!--<lay-button size="sm">删除</lay-button> -->
                </lay-col>
                <lay-col style="text-align:right;">
                   
                   
                  <lay-button prefix-icon="layui-icon-export" suffix-icon=""
                    @click="tableExport"
                    >导出</lay-button>

                </lay-col>
              </lay-row>
              
            </template>
            <template v-slot:debug="{ data }"> 
              <lay-switch
                :model-value="data.debug"
                @change="changeIsDebug($event, data)"
              ></lay-switch>
            </template>
             
            <template v-slot:operator="{data}">
              <lay-button size="xs" 
              border="green" border-style="dashed" 
              @click="ForceLx(data)"
              v-permission="['/htuserlogin/index:auth']"
              >强制离线</lay-button>
              
            </template>
           
          </lay-table>

        </div>

    
  </lay-container>
</template>

<script lang="ts">
import { ref, watch,reactive,onMounted,h } from 'vue'
import { layer } from '@layui/layer-vue'
import MyHttp from "../../library/MyHttp";  
import MyWin from "../../library/MyWin"; 
import MyPage from "../../library/MyPage"; 
import MyAuth from "../../library/MyAuth"; 
import {MyExcel} from "../../library/MyExcel"; 
import {MySimpleExport,TableColumn} from "../../library/MySimpleExport"; 
 
import MyData from '../../library/MyData';

export default {
  setup() {
     
    const baseUrl="/htuserlogin";
    const baseQUrl=baseUrl+"/page";
    const pageName="在线用户";

    const myWin=ref<any>(null);
    const selectedKeys = ref([]) 
    const defaultToolbar = ref(false)
    const page = ref(  new MyPage()  ); 
    
    const columns = [
      // {
      //   title: '复选',
      //   width: '50px',
      //   type: 'checkbox',
      //   fixed: 'left'
      // },
      {
        title: '序号',
        width: '50px' ,
        key: 'rowNo'
      },
      { key:'name',title: '姓名', width: '100px', },
{ key:'htdeptName',title: '机构名称', width: '100px', },
{ key:'loginway',title: '登录方式', width: '100px', },
{ key:'ip',title: 'IP', width: '100px', },
{ key:'browser',title: '浏览器', width: '100px', },
{ key:'crtDt',title: '创建日期', width: '100px', },
{ key:'debug',title: '是否调试', width: '100px', customSlot: 'debug', },

 
       
      {
        title: '操作',
        width: '100px',
        customSlot: 'operator',
        // key: 'operator',
        fixed: 'right'
      }
    ]
    if(!MyAuth.haveUrl(baseUrl+"/index:auth")){
      columns.splice(columns.length-1)
      }
    const dataSource = ref([]);

    const rowClick = function (data: any) {}

    const rowDoubleClick = function (data: any) {}

    const pageChange = function ({ current, limit }: any) {
      if(qm.pageSize!=limit)
      {
        qm.pageSize=limit ;
        qm.page=1;
        query(1); 
      }else{
        query(current)
      }
      

    }
    function toSearch() { 
      query(1);
    }
     
    function toReset() {
      qm.key= '';
      qm.dtb='';
      qm.dte='';       
    }

    const qm=reactive({
      key:"",
      dtb:"",
      dte:"", 
      page: 1,
      pageSize: 10,
      sortFields:"crtDt desc",
      htdeptId:1000,
      state:"在线" 
    }); 
    const query=async (pageIndex:number) =>{
      if(pageIndex){
        page.value.current=pageIndex;
        qm.page=pageIndex;
      }
      var r=await new MyHttp().page(baseQUrl,qm,
        { 
        },
        columns
      ); 
      dataSource.value=r.data.records;          
        page.value.total=r.data.totalRow;
    }
     
    const sortChange = (key: any, sort: number) => {
      qm.sortFields=`${key} ${sort}`;
      query(0);
    }
    const tableExport=async()=>{
     
      var cols=columns as Array<TableColumn>;
      new MySimpleExport().exportExcel(
        {
          qm,
          url:baseQUrl,
          name:pageName,
          columns:cols,
          lx:"page"
        }
        );
    }
    
    const addRow=async()=>{
       
    }
    const updateRow=async(data:any)=>{
       
    }

    const deleteRow=async(data:any)=>{
      let msg=`是否删除 ${data.name} ?`;
      let qm={
        id:data.id
      };
      MyWin.confirm(msg,{
          onOK:async()=>{
            var r= await new MyHttp().post(baseUrl+"/delete",qm,
              { 
              }
            ); 
            query(0);
          }
      })
    }
     
    const ForceLx=async(data:any)=>{
      let msg=`是否强制离线 ${data.name} ?`;
      let inobj={
        id:data.id
      };
      MyWin.confirm(msg,{
          onOK:async()=>{
            var r= await new MyHttp().post(baseUrl+"/ForceLx",inobj,
              { 
              }
            ); 
            query(0);
          }
      })
    }

    const changeIsDebug = async(isChecked: boolean, data: any) => {
      if(!MyAuth.haveUrl(baseUrl+"/index:auth")){
        return;
      }
      let msg=`是否设置 ${data.name} 为调试模式?`;
      if(!isChecked){
        msg=`是否取消 ${data.name} 的调试模式?`;
      }
      let inobj={
        id:data.id,
        debug:isChecked
      };
      MyWin.confirm(msg,{
          onOK:async()=>{
            var r= await new MyHttp().post(baseUrl+"/SetDebug",inobj,
              { 
              }
            ); 
            query(0);
          }
      })
    }

    onMounted(() =>{  
      myWin.value=new MyWin();
      query(1);
       
    });
    return {
      columns,
      dataSource,
      selectedKeys, 
      defaultToolbar,
      page,
      rowClick,
      rowDoubleClick,
      pageChange,
      toReset,
      toSearch,
       

      qm,
      query,
      
      sortChange,
      tableExport,
    
      myWin,
      addRow,
      updateRow,
      deleteRow,

      ForceLx,
      changeIsDebug
    }
  }
}
</script>
